package com.ticket.sass.admin.business.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ticket.sass.admin.business.MemberCardRechargeBusiness;
import com.ticket.sass.admin.dto.input.member.MemberCardRechargeAddInput;
import com.ticket.sass.admin.dto.input.member.MemberCardRechargeListInput;
import com.ticket.sass.admin.dto.input.member.MemberCardRechargeUpdInput;
import com.ticket.sass.admin.dto.output.member.MemberCardRechargeListOutput;
import com.ticket.sass.admin.model.PageModel;
import com.ticket.sass.admin.service.impl.MemberCardRechargeServiceImpl;
import com.ticket.sass.admin.util.EscapeUtil;
import com.ticket.sass.admin.util.FileUtil;
import com.ticket.sass.admin.util.ModelUtil;
import com.ticket.sass.common.entity.tenant.MemberCardRechargeEntity;
import com.ticket.sass.common.exception.enums.ErrorCode;
import com.ticket.sass.common.exception.exception.BadRequestException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ywb
 * @date 2024-03-19 11:37
 */
@Slf4j
@Service
@AllArgsConstructor
public class MemberCardRechargeBusinessImpl implements MemberCardRechargeBusiness {
    final FileUtil fileUtil;
    final ModelUtil modelUtil;
    final MemberCardRechargeServiceImpl service;
   
    @Override
    public PageModel<MemberCardRechargeListOutput> list(MemberCardRechargeListInput input) {
        IPage<MemberCardRechargeEntity> page = service.iPage(input);
        List<MemberCardRechargeListOutput> outputs = page.getRecords().stream().map(MemberCardRechargeListOutput::new).collect(Collectors.toList());
        return new PageModel<>(page.getTotal(), outputs);
    }
    
    @Override
    public void add(MemberCardRechargeAddInput input) {
        if (input.getEnable()){
            if (service.count(new QueryWrapper<MemberCardRechargeEntity>().eq("amount", input.getAmount()).eq("enable", true)) >= 1){
                throw new BadRequestException(
                    ErrorCode.BAD_REQUEST,
                    EscapeUtil.errorMessage("amount", "该充值金额已经存在并且启用！")
                );
            }
        }
        fileUtil.tempFieldPro(input, "cover");
        
        service.save(modelUtil.mapToMap(input, MemberCardRechargeEntity.class));
    }
    
    @Override
    public void upd(MemberCardRechargeUpdInput input) {
        validateId(input.getId());
        
        if (input.getEnable()){
            if (service.count(new QueryWrapper<MemberCardRechargeEntity>().eq("amount", input.getAmount()).eq("enable", true).ne("id", input.getId())) >= 1){
                throw new BadRequestException(
                    ErrorCode.BAD_REQUEST,
                    EscapeUtil.errorMessage("amount", "该充值金额已经存在并且启用！请禁用存在的，再启用！")
                );
            }
        }
        fileUtil.tempFieldPro(input, "cover");
        service.updateById(modelUtil.mapToMap(input, MemberCardRechargeEntity.class));
    }
    
    private void validateId(Long id) {
        if (service.isEmpty(id)) {
            throw new BadRequestException(
                ErrorCode.RECORD_NOT_EXIST,
                ErrorCode.RECORD_NOT_EXIST.getReasonPhrase()
            );
        }
    }
}
